import ctypes
from GetPixelColor import get_pixel
from ContrastRatio import contrast
from PySpyCore import get_cursor_pos, get_hwnd_from_pos, get_title, get_title_p

user32 = ctypes.windll.user32
CopyImage = user32.CopyImage
LoadImageW = user32.LoadImageW
SetSystemCursor = user32.SetSystemCursor
GetDC = user32.GetDC
gdi32 = ctypes.windll.gdi32
GetPixel = gdi32.GetPixel


IMAGE_CURSOR = 2
LR_SHARED = 32768
LR_COPYFROMRESOURCE = 16384
LR_LOADFROMFILE = 16
OCR_NORMAL = 32512


# import time

# # 准备工作
# FILE_NAME_CURSOR = ".\\assets\\target_3.ani"
# hold = LoadImageW(0, OCR_NORMAL, IMAGE_CURSOR, 0, 0, LR_SHARED)
# hsave = CopyImage(hold, IMAGE_CURSOR, 0, 0, LR_COPYFROMRESOURCE)
# hnew = LoadImageW(0, FILE_NAME_CURSOR, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE)
# # 替换光标
# SetSystemCursor(hnew, OCR_NORMAL)
# time.sleep(5);print("正在等待……")
# # 还原光标
# SetSystemCursor(hsave, OCR_NORMAL)
# hsave = CopyImage(hold, IMAGE_CURSOR, 0, 0, LR_COPYFROMRESOURCE)
# hnew = LoadImageW(0, FILE_NAME_CURSOR, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE)
# time.sleep(5);print("正在等待……")
# # 替换光标
# SetSystemCursor(hnew, OCR_NORMAL)
# time.sleep(5);print("正在等待……")
# # 还原光标
# SetSystemCursor(hsave, OCR_NORMAL)
# hsave = CopyImage(hold, IMAGE_CURSOR, 0, 0, LR_COPYFROMRESOURCE)
# hnew = LoadImageW(0, FILE_NAME_CURSOR, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE)
# time.sleep(5);print("正在等待……")


class CursorTool:
    def __init__(self, ani_file_name, ani_white_file_name) -> None:
        self.FILE_NAME_CURSOR = ani_file_name
        self.FILE_NAME_CURSOR_W = ani_white_file_name

        self.__enable_state = True
        self.__modified_state = 0  # 0:未更改;1:黑色光标;2:白色光标

        self.__hold = LoadImageW(0, OCR_NORMAL, IMAGE_CURSOR, 0, 0, LR_SHARED)
        self.hsave = CopyImage(self.__hold, IMAGE_CURSOR, 0, 0, LR_COPYFROMRESOURCE)
        self.hnew = LoadImageW(
            0, self.FILE_NAME_CURSOR, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE
        )
        self.hnew_white = LoadImageW(
            0, self.FILE_NAME_CURSOR_W, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE
        )

    def set_new_cursor(self):
        if self.__enable_state == False:
            print("功能未启用", "set_new_cursor")
            return
        if self.__modified_state == 0 or self.__modified_state == 2:
            if self.__modified_state == 2:
                self.restore_cursor()  # 先恢复为0
            self.__modified_state = 1
            err = SetSystemCursor(self.hnew, OCR_NORMAL)
            self.observe_err(err, "set_new_cursor")

    def __set_new_cursor_white(self):
        if self.__enable_state == False:
            print("工具已关闭", "__set_new_cursor_white")
            return
        if self.__modified_state == 0 or self.__modified_state == 1:
            if self.__modified_state == 1:
                self.restore_cursor()  # 先恢复为0
            self.__modified_state = 2
            err = SetSystemCursor(self.hnew_white, OCR_NORMAL)
            self.observe_err(err, "__set_new_cursor_white")

    def restore_cursor(self):
        if self.__enable_state == False:
            print("工具已关闭", "restore_cursor")
            return
        if self.__modified_state == 1 or self.__modified_state == 2:
            self.__modified_state = 0
            err = SetSystemCursor(self.hsave, OCR_NORMAL)
            self.__hold = LoadImageW(0, OCR_NORMAL, IMAGE_CURSOR, 0, 0, LR_SHARED)
            self.hsave = CopyImage(self.__hold, IMAGE_CURSOR, 0, 0, LR_COPYFROMRESOURCE)
            self.hnew = LoadImageW(
                0, self.FILE_NAME_CURSOR, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE
            )
            self.hnew_white = LoadImageW(
                0, self.FILE_NAME_CURSOR_W, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE
            )
            self.observe_err(err, "restore_cursor")

    def moving_mouse(self):
        cursor_pos = get_cursor_pos()
        x = cursor_pos[0]
        y = cursor_pos[1]
        userless = self.__set_new_cursor_white
        c = get_pixel(x, y)
        cr = contrast(0, c)
        if cr < 4.5:
            # print("背景颜色与光标颜色较近，不易分辨×")
            self.__set_new_cursor_white()
        else:
            # print("背景颜色与光标颜色较远，容易分辨√")
            self.set_new_cursor()

    def observe_err(self, err, fun_name):
        if err == 0:
            self.__enable_state = False
            if fun_name == "set_new_cursor":
                print("设置新光标失败，工具已关闭", "set_new_cursor")
            elif fun_name == "__set_new_cursor_white":
                print("设置白光标失败，工具已关闭", "__set_new_cursor_white")
            elif fun_name == "restore_cursor":
                print("恢复原光标失败，工具已关闭", "restore_cursor")


# https://contrast-ratio.com/
# AA级别要求文本视觉呈现/文本图像至少要有4.5:1的对比度
# 尝试遵循该规则，纯黑色光标和光标下颜色的对比度小于4.5时，改变光标颜色为白色
